//
//  UIButton+Extension.swift
//  sinaDemo
//
//  Created by mac on 15/12/16.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit


extension UIButton {
    /**
    返回一个有背景视图的按钮
    
    - parameter image:     按钮内部的图片
    - parameter backImage: 背景图片
    */
    convenience init(image: String? = nil, backImage: String) {
        self.init()
        if image != nil {
            setImage(UIImage(named: image!), forState: UIControlState.Normal)
            setImage(UIImage(named: image! + "_highlighted"), forState: UIControlState.Highlighted)
        }
        setBackgroundImage(UIImage(named: backImage), forState: UIControlState.Normal)
        setBackgroundImage(UIImage(named: backImage + "_highlighted"), forState: UIControlState.Highlighted)
        sizeToFit()
    }
    
    /**
    返回一个带背景视图+文字+颜色的按钮
    
    - parameter image:     按钮内部的图片
    - parameter backImage: 按钮的背景图片呢
    - parameter title:     按钮的标题
    - parameter color:     按钮标题的颜色
    */
    convenience init(image: String, backImage: String?, title: String, color: UIColor, fontSize: CGFloat) {
        self.init()
        setImage(UIImage(named: image), forState: UIControlState.Normal)
        if backImage != nil {
            setBackgroundImage(UIImage(named: backImage!), forState: UIControlState.Normal)
        }
        setTitle(title, forState: UIControlState.Normal)
        setTitleColor(color, forState: UIControlState.Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        sizeToFit()
    }
}